package com.czk.config;

import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * 在Spring初始化完毕后后调用接口onStartup()方法
 * servlet规范
 * 利用spi机制，加载实现类的实例并调用onStartup()方法
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/27 17:19
 */
@Component
public class MyInit extends SpringContextHolder implements WebApplicationInitializer {
    /**
     * 获取tomcat的上下文
     *
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("调用onStartup方法了");
        // 将前端控制器加载到tomcat上下文中
        ServletRegistration.Dynamic app = servletContext.addServlet("dispatcher", getServlet());
        app.setLoadOnStartup(1);
    }
}
